package org.xg.study.pd;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;

import java.util.List;

/**
 * @author CMG
 * @version 1.0
 * @description
 * @cLassName Cmd
 * @date 2021/9/16 14:52
 **/
public class Cmd {
    @Parameter(names = {"-?", "-help"}, description = "print help message", order = 3, help = true)
    public boolean helpFlag = false;

    @Parameter(names = "-version", description = "print version and exit", order = 2)
    public boolean versionFlag = false;

    @Parameter(names = {"-cp", "-classpath"}, description = "classpath", order = 1)
    public String classpath;

    @Parameter(description = "main class and args")
    public List<String> mainClassArgs;

    public boolean ok;

    public String getMainClass() {
        return mainClassArgs != null && !mainClassArgs.isEmpty() ? mainClassArgs.get(0) : null;
    }

    public List<String> getAppArgs() {
        return mainClassArgs != null && mainClassArgs.size() > 1 ? mainClassArgs.subList(1, mainClassArgs.size()) :
                null;
    }

    public static Cmd parse(String[] argv) {
        Cmd args = new Cmd();
        JCommander cmd = JCommander.newBuilder().addObject(args).build();
        cmd.parse(argv);
        args.ok = true;
        return args;
    }
}
